package board.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import board.dao.BoardDAO;
import board.util.Utilities;
import board.vo.ListVO;
import board.vo.ReplyVO;
import board.vo.SportsBoardVO;
import board.vo.SportsPagingVO;
import board.vo.TeamVO;



public class BoardBusinessService {
	
	private BoardDAO dao;
	
	public void setBoardDAO(BoardDAO dao){
		this.dao = dao;
	}
	
	
	public void writeBoardContent(SportsBoardVO sbv) throws SQLException{		
		dao.insertContent(sbv);		
	}


	public List allSearchBoard() throws SQLException{
		return dao.allSearchBoard();		
	}


	public ListVO selectContentList(int page,int typeNo) {
		int totalContent = dao.selectTotalCount(typeNo);
		SportsPagingVO spv = new SportsPagingVO(totalContent, page);
		spv.setTypeNo(typeNo);
		ArrayList list = dao.selectContentList(spv);
		if(list.size() == 0){
			page = page -1;
			spv.setNowPage(page);
			list = dao.selectContentList(spv);
		}
		ListVO lvo = new ListVO(list, spv);
		return lvo;
	}
	
	public ListVO searchFindTitle(int page, int typeNo, String searchTxt) {
		int totalContent = dao.selectTotalCountTitle(typeNo,searchTxt);
		SportsPagingVO spv = new SportsPagingVO(totalContent, page);
		spv.setTypeNo(typeNo);
		spv.setTempStr(searchTxt);
		ArrayList list = dao.searchFindTitle(spv);
		if(list.size() == 0){
			page = page -1;
			spv.setNowPage(page);
			list = dao.selectContentList(spv);
		}
		ListVO lvo = new ListVO(list, spv);
		return lvo;		
	}

	public ListVO searchFindContent(int page, int typeNo, String searchTxt) {
		int totalContent = dao.selectTotalCountContent(typeNo,searchTxt);
		SportsPagingVO spv = new SportsPagingVO(totalContent, page);
		spv.setTypeNo(typeNo);
		spv.setTempStr(searchTxt);
		ArrayList list = dao.searchFindContent(spv);
		if(list.size() == 0){
			page = page -1;
			spv.setNowPage(page);
			list = dao.selectContentList(spv);
		}
		ListVO lvo = new ListVO(list, spv);
		return lvo;
	}


	public ListVO searchFindName(int page, int typeNo, String searchTxt) {
		int totalContent = dao.selectTotalCountNickName(typeNo,searchTxt);
		SportsPagingVO spv = new SportsPagingVO(totalContent, page);
		spv.setTypeNo(typeNo);
		spv.setTempStr(searchTxt);
		ArrayList list = dao.searchFindName(spv);
		if(list.size() == 0){
			page = page -1;
			spv.setNowPage(page);
			list = dao.selectContentList(spv);
		}
		ListVO lvo = new ListVO(list, spv);
		return lvo;
	}


	public ListVO searchFindTC(int page, int typeNo, String searchTxt) {
		int totalContent = dao.selectTotalCountTC(typeNo,searchTxt);
		SportsPagingVO spv = new SportsPagingVO(totalContent, page);
		spv.setTypeNo(typeNo);
		spv.setTempStr(searchTxt);
		ArrayList list = dao.searchFindTC(spv);
		if(list.size() == 0){
			page = page -1;
			spv.setNowPage(page);
			list = dao.selectContentList(spv);
		}
		ListVO lvo = new ListVO(list, spv);
		return lvo;
	}


	public SportsBoardVO searchBoardByNo(int no) {
		return dao.searchBoardByNo(no);				
	}

	

	public int deleteWrite(String boardNo) {	
		return dao.deleteWrite(boardNo);
	}


	public void writeModify(SportsBoardVO sbv) {
		String boardDate = Utilities.getNow();
		sbv.setBoardDate(boardDate);
		sbv.setBoardContent(Utilities.changeTagToText(sbv.getBoardContent()));
		dao.writeModify(sbv);		
	}


	public void insertReply(ReplyVO rvo) {
		dao.insertReply(rvo);
		
	}


	public ArrayList findReplyByNo(int no) {
		return dao.findReplyByNo(no);
	}


	public String searchJoinNickNameById(String id) {
		return dao.searchJoinNickNameById(id);
	}


	public void deleteReply(int replyNo) {
		dao.deleteReply(replyNo);
	}


	public ArrayList searchTeamInfoByType(int teamType) throws Exception{
		return dao.searchTeamInfoByType(teamType);
	}
	public ArrayList searchAllTeamInfoByType() throws Exception{
		return dao.searchAllTeamInfoByType();
	}	



	public TeamVO findTeamInfoByNo(int teamNo) throws Exception{		
		return dao.findTeamInfoByNo(teamNo);
	}


	public void boardCountByNo(int no) {
		dao.boardCountByNo(no);		
	}


	public int selectBoardNo() throws Exception{
		return dao.selectBoardNo();
	}


	public String typeNameByNo(int typeNo) throws Exception{
		return dao.typeNameByNo(typeNo);
	}



}










